/*
 * Copyright (C) 2012 Evgeniy Egorov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.diasled.model.exam;

import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Evgeniy Egorov
 */
public class GinElemsTableModel extends AbstractTableModel {

    final private int outlinesCount;
    final private boolean[][] elems;
    final private GinCanvas canvas;

    public boolean[][] getElems() {
        return elems;
    }

    public GinElemsTableModel(int outlinesCount, GinCanvas canvas) {
        this.outlinesCount = outlinesCount;
        elems = new boolean[2][outlinesCount];
        for (int i = 0; i < elems.length; i++) {
            for (int j = 0; j < elems[0].length; j++) {
                elems[i][j] = true;
            }
        }
        this.canvas = canvas;
    }

    @Override
    public int getRowCount() {
        return outlinesCount;
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {

            return (rowIndex % 3 == 0 ? "Красный " : (rowIndex % 3 == 1 ? "Синий " : "Зеленый")) + (rowIndex + 1);
        }
        return elems[columnIndex - 1][rowIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        elems[columnIndex - 1][rowIndex] = (boolean) aValue;
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                canvas.repaint();
            }
        });
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        }
        return Boolean.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "№";
            case 1:
                return "ГИН";

            case 2:
                return "Шаги";
            default:
                throw new AssertionError();
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex != 0;
    }
}
